package com.zcxx.cotton.pick;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zcxx.cotton.pick.domain.PickInfo;
import com.zcxx.cotton.pick.service.IPickInfoService;
import com.zcxx.cotton.sale.domain.BuySubsidyRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 采摘信息Controller
 *
 * @author ruoyi
 * @date 2021-05-16
 */
@RestController
@RequestMapping("/pick/PickInfo")
public class PickInfoController extends BaseController {


    @Autowired
    private IPickInfoService pickInfoService;


    /**
     * 查询采摘信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PickInfo pickInfo) {
        startPage();
        startOrderBy();
        if (pickInfo.getSampleNumber() != null) {
            String sampleNumber = pickInfo.getSampleNumber().toUpperCase();
            pickInfo.setSampleNumber(sampleNumber);
        }
        List<PickInfo> list = pickInfoService.selectPickInfoList(pickInfo);
        return getDataTable(list);
    }


    /**
     * 查询全部采摘信息列表
     */
    @GetMapping("/allList")
    public TableDataInfo allList(PickInfo pickInfo) {
        List<PickInfo> list = pickInfoService.selectPickInfoList(pickInfo);
        return getDataTable(list);
    }

    /**
     * 导出采摘信息列表
     */
    @Log(title = "采摘信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,PickInfo pickInfo) {
        try {
            List<PickInfo> list = pickInfoService.selectPickInfoList(pickInfo);
            ExcelUtil<PickInfo> util = new ExcelUtil<PickInfo>(PickInfo.class);
            util.exportExcel(response, list, "采摘信息数据");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增保存采摘信息
     */
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody PickInfo pickInfo) {
        return toAjax(pickInfoService.save(pickInfo));
    }

    /**
     * 修改采摘信息
     */
    @GetMapping("/edit/{pickInfoId}")
    public AjaxResult edit(@PathVariable("pickInfoId") Long pickInfoId) {
        PickInfo pickInfo = pickInfoService.getById(pickInfoId);
        return AjaxResult.success(pickInfo);
    }

    /**
     * 修改保存采摘信息
     */
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody PickInfo pickInfo) {
        return toAjax(pickInfoService.updateById(pickInfo));
    }

    /**
     * 删除采摘信息
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        return toAjax(pickInfoService.deletePickInfoById(ids));
    }

}
